﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFCanvas.CustomControls;
using ExORMDesginer.Models;
using System.Collections.ObjectModel;
using DiagramDesigner;
using ExORMDesginer.Plugin;
namespace ExORMDesginer.UserControls
{
    /// <summary>
    /// Interaction logic for CanvasDesginer.xaml
    /// </summary>
    public partial class CanvasDesginer : UserControl
    {
        public CanvasDesginer()
        {
            InitializeComponent();
        }

        public DesignerCanvas DesignerCanvas
        {
            get
            {
                return (DesignerCanvas)MyDesignerCanvas;
            }
        }

    




        public ObservableCollection<DbEntity> ItemsSource
        {
            set
            {
                SetValue(ItemsSourceProperty,value);
            }
            get
            {
                return (ObservableCollection<DbEntity>)GetValue(ItemsSourceProperty);
            }
        }

        public static readonly DependencyProperty ItemsSourceProperty =
                DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<DbEntity>), typeof(CanvasDesginer), new PropertyMetadata(ItemsSourceChanged));


        static void ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            

            var that = d as CanvasDesginer;
            that.MyDesignerCanvas.Children.Clear();
            var items = e.NewValue as ObservableCollection<DbEntity>;
            if (items == null)
                return;

            int px= 50;
            int x = px;
            int i = 0;
            int y = 35;
            int c = (int)Math.Sqrt(items.Count) + 2;
            foreach(DbEntity item in items)
            {
               
                DrawItem di = new DrawItem() { Text = item.DisplayName, ControlType = NodeType.QueryNode.ToString(), Tag =item };
                di.Style = that.FindResource(di.ControlType) as Style;

                that.MyDesignerCanvas.CanvasAddNode(di, new Point(x, y), Guid.Empty);
                x += 185;
                i++;
                if ( i % c == 0)
                {
                    x = px;
                    y += 205;
                }
            }
         
        }



      
    }
}
